package com.example.demo.service.impl;

import com.example.demo.mapper.DeptMapper;
import com.example.demo.pojo.Dept;
import com.example.demo.pojo.DeptExample;
import com.example.demo.service.DeptService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class DeptServiceImpl implements DeptService {

    @Resource
    private DeptMapper deptMapper;

    @Override
    public List<Dept> selectAllDept(String name) {
        DeptExample example = new DeptExample();
        if (name != null && !"".equals(name)) {
            example.createCriteria().andNameLike("%"+name+"%");
        }
        return deptMapper.selectByExample(example);
    }

    @Override
    public List<Dept> selectLinkDeptByIds(Set<BigDecimal> idList, Set<BigDecimal> parentList) {
        ArrayList<Dept> result = new ArrayList<>();
        DeptExample example = new DeptExample();
        //查询对应节点的子节点
        DeptExample.Criteria criteria = example.createCriteria();
        criteria.andParentidIn(new ArrayList<>(parentList));
        List<Dept> childDepts = deptMapper.selectByExample(example);
        //查询对应节点的父节点
        example = new DeptExample();
        criteria = example.createCriteria();
        criteria.andIdIn(new ArrayList<>(idList));
        List<Dept> parentDepts = deptMapper.selectByExample(example);
        result.addAll(childDepts);
        result.addAll(parentDepts);
        return result;
    }
}
